package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Hgxx;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class HgxxForm extends BasePage implements Serializable {
    private GenericManager<Hgxx, Long> hgxxManager;
    private Hgxx hgxx = new Hgxx();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setHgxxManager(GenericManager<Hgxx, Long> hgxxManager) {
        this.hgxxManager = hgxxManager;
    }

    public Hgxx getHgxx() {
        return hgxx;
    }

    public void setHgxx(Hgxx hgxx) {
        this.hgxx = hgxx;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        hgxxManager.remove(hgxx.getId());
        addMessage("hgxx.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            hgxx = hgxxManager.get(id);
        } else {
            hgxx = new Hgxx();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (hgxx.getId() == null || hgxx.getId() == 0);
        hgxx.setUsername(getRequest().getRemoteUser());
        hgxx.setOptdate(new Date());
        hgxx.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        
        hgxxManager.save(hgxx);

        String key = (isNew) ? "hgxx.added" : "hgxx.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = hgxx.getId();
            return "edit";
        }
    }
} 